package org.example.controller;

import jakarta.annotation.Resource;
import org.example.pojo.Notifies;
import org.example.pojo.NotifiesDTO;
import org.example.service.NotifyService;
import org.example.utils.R;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 通知管理
 * @Author:曹震
 * @Date: 2024/12/1 16:25
 */
@RestController
@RequestMapping("notify")

public class NotifyController {
    @Resource
    private NotifyService service;
    //发布任务
    @PostMapping("add")
    public R addNotify(@RequestBody NotifiesDTO notifyDto) {
        try {
            service.addNotify(notifyDto);
            return R.successful("通知发布成功", null);
//            return ResponseEntity.ok("通知发布成功");
        } catch (Exception e) {
            return R.error("发布失败", null);
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("发布通知失败");
        }
    }
    //查询发布的通知,根据我们的筛选条件来进行
    @GetMapping("list/{pageSize}/{currentPage}")
    public R listNotify(@PathVariable int pageSize,
                        @PathVariable int currentPage,
                        @RequestParam(value = "status",required = false,defaultValue = "0") int status){
        R r = service.listNotify(pageSize, currentPage,status);
        return r;
    }
    //根据id获取相对应的消息通知
    @PostMapping("editOne")
    public R editNotify1(@RequestParam Integer notifyId){
        Notifies notifies = service.editNotify1(notifyId);
        if (notifies==null){
            return R.empty("没有找到相对应的通知", null);
        }else {
            return R.successful(null, notifies);
        }
    }
    //编辑我们的任务，先通过编辑一，找到我们那个消息，然后更新
    @PostMapping("editTwo")
    public R editNotify2(@RequestParam Integer notifyId,@RequestBody(required = false) NotifiesDTO notifiesDTO){
        R r = service.editNotify2(notifyId, notifiesDTO);
        return r;
    }
    //撤销通知 将通知状态修改
    @PostMapping("revoke")
    public R revokeNotify(int notifyId){
        R r = service.cacelNotify(notifyId);
        return r;
    }
    //删除通知
    @DeleteMapping("delete")
    public R deleteNotify(@RequestParam("notifyId") int notifyId){
        R delete = service.delete(notifyId);
        return delete;
    }
}
